package com.sz.netty.controller;

import com.sz.netty.service.DeviceCommandService;
import com.sz.core.common.entity.ApiResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Controller for sending commands to connected devices
 */
@Slf4j
@RestController
@RequestMapping("/device/command")
@Tag(name = "Device Command API")
@RequiredArgsConstructor
public class DeviceCommandController {
    
    private final DeviceCommandService deviceCommandService;
    
    @PostMapping("/display/{deviceId}")
    @Operation(summary = "Send remote display content command to a device")
    public ApiResult<Boolean> sendRemoteDisplayContent(
            @PathVariable String deviceId,
            @RequestParam String content) {
        try {
            boolean result = deviceCommandService.sendRemoteDisplayContent(deviceId, content);
            if (result) {
                return ApiResult.success(true, "Command sent successfully");
            } else {
                return ApiResult.success(false, "Failed to send command to device");
            }
        } catch (Exception e) {
            log.error("Error sending remote display content to device {}: {}", deviceId, e.getMessage(), e);
            return ApiResult.success(false, "Error sending command: " + e.getMessage());
        }
    }
    
    @PostMapping("/display/bytes/{deviceId}")
    @Operation(summary = "Send remote display content command to a device using byte array")
    public ApiResult<Boolean> sendRemoteDisplayContentBytes(
            @PathVariable String deviceId,
            @RequestBody byte[] content) {
        try {
            boolean result = deviceCommandService.sendRemoteDisplayContent(deviceId, content);
            if (result) {
                return ApiResult.success(true, "Command sent successfully");
            } else {
                return ApiResult.success(false, "Failed to send command to device");
            }
        } catch (Exception e) {
            log.error("Error sending remote display content to device {}: {}", deviceId, e.getMessage(), e);
            return ApiResult.success(false, "Error sending command: " + e.getMessage());
        }
    }
    
    @GetMapping("/connected/{deviceId}")
    @Operation(summary = "Check if a device is currently connected")
    public ApiResult<Boolean> isDeviceConnected(@PathVariable String deviceId) {
        try {
            boolean connected = deviceCommandService.isDeviceConnected(deviceId);
            return ApiResult.success(connected, connected ? "Device is connected" : "Device is not connected");
        } catch (Exception e) {
            log.error("Error checking device connection status for device {}: {}", deviceId, e.getMessage(), e);
            return ApiResult.success(false, "Error checking device connection status: " + e.getMessage());
        }
    }
    
    @GetMapping("/connected")
    @Operation(summary = "Get list of all connected devices")
    public ApiResult<String[]> getConnectedDevices() {
        try {
            String[] devices = deviceCommandService.getConnectedDevices();
            return ApiResult.success(devices, "Retrieved " + devices.length + " connected devices");
        } catch (Exception e) {
            log.error("Error retrieving connected devices: {}", e.getMessage(), e);
            return ApiResult.success(new String[0], "Error retrieving connected devices: " + e.getMessage());
        }
    }
}